package prefuse.tp.svn;

import java.awt.event.MouseEvent;
import javax.swing.ToolTipManager;

import prefuse.Display;
import prefuse.controls.ControlAdapter;
import prefuse.visual.VisualItem;

public class GraphRadial_ControlTooltip extends ControlAdapter {

	private String[] fields;
	private String[] texts;
	private StringBuffer sbuf;

	int dismissDelay = Integer.MAX_VALUE;

	public GraphRadial_ControlTooltip(String[] texts, String[] fields) {
		this.texts = texts;
		this.fields = fields;
		sbuf = new StringBuffer();
		ToolTipManager.sharedInstance().setInitialDelay(50);
	}

	public void itemEntered(VisualItem vi, MouseEvent e) {
		String value;
		ToolTipManager.sharedInstance().setDismissDelay(dismissDelay);
		sbuf.delete(0, sbuf.length());
		sbuf.append("<html>" +
					"<style type=\"text/css\">"+
					"table#sample {" +
					"background-color:#FFFFFF;"+
					"border: solid #000 3px;"+
					"}"+
					"table#sample td {"+
					"padding: 5px;"+
					"border: solid #000 1px;}"+
					".data {"+
					"color: #000000;"+
					"text-align: justify;"+
					"font-size: 0.9em;"+
					"background-color: #CCCCCC;}"+
					".toprow {"+
					"text-align: center;"+
					"background-color: #FFFFCC;}"+
					".leftcol {"+
					"font-weight: bold;"+
					"text-align: left;"+
					"background-color: #CCCCCC;}"+
					"<body><table id=sample cellSpacing=2>");

		boolean someInfo = false;
		for (int i = 0; i < fields.length; i++) {
			if (vi.canGetString(fields[i])) {
				value = vi.getString(fields[i]);
				if (value != null && value.length() > 0) {
					if(value.length()>50) { value = backline(value,50);	}
					sbuf.append("<tr class=toprow><td class=leftcol><b>");
					sbuf.append(texts[i] + "</b></td><td>");
					sbuf.append(value);
					someInfo = true;
				}
				sbuf.append("</td></tr>");
			}
		}

		if (someInfo) {
			sbuf.append("</table>");
			sbuf.append("<hr size=1 width=\"97%\"><div align=\"right\"><font color=\"#000000\">Esc to close&nbsp;</font></div>");
			sbuf.append("</body></html>");

			Display disp = (Display) e.getSource(); //the display on which the event occured
			disp.setToolTipText(sbuf.toString());
		}

	}

	public void itemExited(VisualItem item, MouseEvent e) {
		Display disp = (Display) e.getSource();
		disp.setToolTipText(null);
	}
	
	public String backline(String value, int taille){
		String[] p = new String [50];
		String texte = new String(value);
		int start = 0, end = 0, line = 0;
		while (end  < texte.length()){
			start 	= line*taille;
			if ((line + 1)*taille < texte.length()){
				end	= (line + 1)*taille;
			}
			else end = texte.length();
			p[line] = texte.substring(start,end);
			line++;
		}
		value = "";
		for (int i = 0; i < line; i++){
			value += p[i]+"<br/>";
		}
		return value;
	}

} // end of class DemoControlToolTip